<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持webSocket编程
    if(window.WebSocket){
        //go on
        socket=new WebSocket("ws://localhost:7000/hello")
        //相当于channelRead0,event 收到服务器端回送的消息
        socket.onmessage=function(event){
            var rt = document.getElementById("responseText");
            rt.value=rt.value+"\n"+event.data;
        }
        //相当于连接开启
        socket.onopen=function (event){
            var rt = document.getElementById("responseText");
            rt.value="连接开启";
        }
        socket.onclose=function (event){
            var rt = document.getElementById("responseText");
            rt.value=rt.value+"\n连接关闭";
        }
    }else{
        alert("当前浏览器不支持webSocket")
    }
    //发送消息到服务器
    function send(message){
        if(!socket){ //修改了这里，先判断socket是否已创建
            return;
        }
        if(socket.readyState == WebSocket.OPEN){
            //通过socket发送消息
            socket.send(message);
        }else{
            alert("连接没有开启")
        }
    }
</script>
<form onsubmit="return false">
    <textarea id="msg" name="msg" style="height: 300px;width: 300px" ></textarea>
    <input type="button" value="发送消息" onclick="send(document.getElementById('msg').value)">
    <textarea id="responseText" style="height: 300px;width: 300px"></textarea>
    <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
</form>
</body>
</html>